package net.sppan.base.task;

import java.util.concurrent.Executor;
import java.util.concurrent.Executors;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.SchedulingConfigurer;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;
/**
 * 定时任务并发 配置
 */
@Configuration
@EnableScheduling
public class ScheduledConfig implements SchedulingConfigurer {
	
	@Override
	public void configureTasks(ScheduledTaskRegistrar scheduledTaskRegistrar) {
	    scheduledTaskRegistrar.setScheduler(setTaskExecutors());
	}

	/**
	 * 配置定时任务 线程数
	 * @return
	 */
	@Bean(destroyMethod="shutdown")
	public Executor setTaskExecutors(){
		/**
		 *  一种是 newCachedThreadPool() ，一种是 newFixedThreadPool()， 一种是 newSingleThreadExecutor()
		 *  其中newScheduledThreadPool() 线程池的采用的队列是延迟队列。newScheduledThreadPool() 线程池的特性是定时任务能够定时或者周期性的执行任务
		 */
	    return Executors.newScheduledThreadPool(3); // 3个线程来处理。
	}
}
